import { useEffect,FC } from "react"
import {history} from "umi"
import { useNavigate, useRoutes, useLocation } from "react-router-dom"
// import useStorage from "@/hooks/useStorage"

type RouteObj = {
    path:string,
    element:React.ReactNode,
    children?:RouteObj[]
}

// 定义类型
interface Iprops {
    routes: RouteObj[];
    storageType: string;
}

const useRoutesWithGuard:FC<Iprops> = (props) => {

    // let token: string | null = sessionStorage.getItem(key);
    // 解构要用到数据
    let {routes,storageType} = props
    
    let token = localStorage.getItem('qf-token')
    // let token: string | null = window[storage].getItem(key);
    // let [userInfo,method] = useStorage('qf-userInfo')

    // let navigate = useNavigate()
    // 引用路由元素
    let Element = useRoutes(routes)

    let location = useLocation()

    useEffect(() => {
        if (!token) {
            //没有token,跳转到login
            //如果访问的页面不是login 就跳转到login
            if (location.pathname !== "/login") {
                history.push('/login')
            }
        }

        // if(location.pathname === '/login'){
        //     methods.rm()
        //     method.rm()
        // }
    },[location])

    

    return Element
}

export default useRoutesWithGuard





